package com.qykj.ddhing.pda.demo.arms.test1;

import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.RxLifecycleUtils;
import com.qykj.ddhing.pda.common.LogUtil;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import me.jessyan.rxerrorhandler.handler.RetryWithDelay;

/**
 * author : 0000
 * desc   :
 * time   : 2018/07/19
 * updateTime : 2018/07/19
 */
@ActivityScope
public class LoginPresenter extends BasePresenter<UserContract.Model, UserContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    public LoginPresenter(UserContract.Model model, UserContract.View rootView) {
        super(model, rootView);
    }
    //这里定义业务方法,相应用户的交互
    public void requestUsers(final boolean pullToRefresh) {
        mModel.getUsers(mRootView.getUIdataUserInfo().getName(),mRootView.getUIdataUserInfo().getPws()) .subscribeOn(Schedulers.io())
                .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                .doOnSubscribe(disposable -> {
                    if (pullToRefresh)
                    LogUtil.getLogUtil_LXF().d("显示下拉刷新的进度条");
                    else
                    LogUtil.getLogUtil_LXF().d("显示上拉加载更多的进度条");
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    if (pullToRefresh)
                    LogUtil.getLogUtil_LXF().d("隐藏下拉刷新的进度条");
                    else
                    LogUtil.getLogUtil_LXF().d("隐藏上拉加载更多的进度条");
                })
//                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<User>(mErrorHandler) {
                    @Override
                    public void onNext(User user) {
                        mRootView.updateUI(user);
                        LogUtil.getLogUtil_LXF().d("user.toString():"+user.toString());
                    }

                });
    }
}
